// server.js
const express = require("express");
const app = express();
const port = 3000; // 你可以选择任何未被占用的端口
const cors = require("cors"); // 引入cors模块

app.use(cors()); // 这允许所有来源的跨域请求，根据实际情况调整选项
app.use(express.json()); // 用于解析JSON格式的请求体

app.get("/api/data", (req, res) => {
  console.log(req.query, "req.query");
  res.send({
    code: "success",
    msg: "成功",
    data: {
      value: req.query.name,
    },
  });
});

app.post("/api/updateData", (req, res) => {
  console.log(req.body, "req.body");
  res.send({
    code: "success",
    msg: "成功",
    data: req.body.data.value + "这是post请求的",
  });
});

app.post("/deploy", (req, res) => {
  console.log(req.body, "req.body");
  res.send({
    code: "success",
    msg: "成功",
    data: req.body.data.value + "这是post请求的",
  });
});

app.post("/bladex/data", (req, res) => {
  console.log(req.body, "req.body");
  res.send({
    code: "success",
    msg: "成功",
    data: {
      value: new Date().getTime().toString(),
    },
  });
});

app.get("/bladex/data", (req, res) => {
  console.log(req.query, "req.body");
  res.send({
    code: "success",
    msg: "成功",
    data: {
      value: req.query.name
        ? `传过来的是这个=>${JSON.stringify(req.query) || ""}`
        : "没有值传过来",
    },
  });
});

app.post("/bladex/getParams", (req, res) => {
  console.log(req.body, "req.body");
  res.send({
    code: "success",
    msg: "成功",
    data: {
      value: req.body.name
        ? `传过来的是这个=>${JSON.stringify(req.body) || ""}`
        : "没有值传过来",
      params: JSON.stringify(req.body),
    },
  });
});

// 启动服务器
app.listen(port, () => {
  console.log(`Server is running at http://localhost:${port}`);
});
